﻿using rmTest.Common.ConstUtil;
using rmTest.First.ServiceInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace rmTest.First.ServiceFactory
{
    public class SimpleFactory
    {
        private static IBaseService dbService = null;
        /// <summary>
        /// 工厂初始化时，获取Config配置的服务信息，通过反射创建对应实例，并设置服务接口的单例
        /// </summary>
        static SimpleFactory()
        {
            if (dbService == null)
            {
                string dllName = ConfigurationArgs.DbHelperType.Split(',')[0];
                string serviceName = ConfigurationArgs.DbHelperType.Split(',')[1];
                Assembly assembly = Assembly.Load(dllName);
                Type serviceType = assembly.GetType(serviceName);
                dbService = Activator.CreateInstance(serviceType) as IBaseService;
            }
        }

        public static IBaseService CreateService()
        {
            return dbService;
        }
    }
}
